package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.config.Contants;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.common.utils.SpUtils;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.mvp.contract.LoadingContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;


/**
 * 首页
 *
 * @author 陈浩
 * @version 1.0.0
 */
public class LoadingPresenter extends BasePresenter<LoadingContract.View>
        implements LoadingContract.Presenter {

    public LoadingPresenter(LoadingContract.View view) {
        // 初始化数据仓库
        super(view);
    }


    @Override
    public void start() {
        super.start();

        // 加载网络数据
//        UserHelper.refreshContacts();

    }
//

    @Override
    public void getMainListData(final Context context, String longitude, String latitude, int page) {
        EasyHttp.post(UrlConfig.SEARCH_MERCHANT_BY_SITE_URL)
                .cacheKey(UrlConfig.SEARCH_MERCHANT_BY_SITE_URL)
                .params("longitude", longitude)
                .params("latitude", latitude)
                .params("page", page + "")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }
                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx","json:"+json);

                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
//                                setError(resultMessage);
                                return;
                            }
//                            List<CreditTourismInfoModel> list = JSON.parseArray(data, CreditTourismInfoModel.class);
                            SpUtils.put(context, Contants.MAIN_JSON,data);
//                            if (getView() != null) {
//                                getView().getMainListDataSuccess(data);
////                                setError(R.string.login_success_text);
//                            }
                        } catch (Exception e) {
//                            setError(R.string.loadig_failed_text);
                        }

                    }
                });
    }
}
